package com.tools.web.work.captcha;

import com.tools.common.exception.IORuntimeException;
import com.tools.common.object.Note;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

/**
 * 点选验证码的构建实例
 * */
@Note("点选验证码的构建实例")
public class ClickCaptchaBuilder {

    @Note("验证码的背景图片")
    private BufferedImage background;

    @Note("验证码背景图的自定义宽度（不能大于图片本身的宽）")
    private int backgroundWidth = -1;

    @Note("验证码背景图的自定义高度（不能大于图片本身的高）")
    private int backgroundHeight = -1;

    @Note("生成文字的个数")
    private int numberOfWord = 3;

    @Note("生成文字的字体样式名称")
    private String fontStyle = "华文行楷";

    @Note("图片的后缀名")
    private String imageSuffix = "PNG";

    /* ********************************************************************************************
     *
     *          构造器
     *
     * *********************************************************************************************
     * */

    public ClickCaptchaBuilder(BufferedImage background) {
        this.setBackground(background);
    }

    public ClickCaptchaBuilder(InputStream imageStream) {
        this.setBackground(imageStream);
    }

    public ClickCaptchaBuilder(File imageFile) {
        this.setBackground(imageFile);
    }

    public ClickCaptchaBuilder(String imageFilePath) {
        this.setBackground(imageFilePath);
    }


    public static ClickCaptchaBuilder build(BufferedImage background) {
        return new ClickCaptchaBuilder(background);
    }

    public static ClickCaptchaBuilder build(InputStream imageStream) {
        return new ClickCaptchaBuilder(imageStream);
    }

    public static ClickCaptchaBuilder build(File imageFile) {
        return new ClickCaptchaBuilder(imageFile);
    }

    public static ClickCaptchaBuilder build(String imageFilePath) {
        return new ClickCaptchaBuilder(imageFilePath);
    }

    /* ********************************************************************************************
     *
     *          Getter / Setter
     *
     * *********************************************************************************************
     * */

    public BufferedImage getBackground() {
        return background;
    }

    public void setBackground(BufferedImage background) {
        if(background == null) throw new NullPointerException("验证码的背景图不能为 null");
        this.background = background;
    }

    public void setBackground(InputStream imageStream) {
        if(imageStream == null) throw new NullPointerException("验证码的背景图的字节输入流不能为 null");
        try {
            this.background = ImageIO.read(imageStream);
        } catch (IOException e) {
            throw new IORuntimeException(e);
        }
    }

    public void setBackground(File imageFile) {
        if(imageFile == null) throw new NullPointerException("验证码的背景图的文件实例不能为 null");
        try {
            this.background = ImageIO.read(imageFile);
        } catch (IOException e) {
            throw new IORuntimeException(e);
        }
    }


    public void setBackground(String imageFilePath) {
        if(imageFilePath == null || imageFilePath.isEmpty()) throw new NullPointerException("验证码的背景图的路径不能为空");
        try {
            this.background = ImageIO.read(new File(imageFilePath));
        } catch (IOException e) {
            throw new IORuntimeException(e);
        }
    }

    public int getBackgroundWidth() {
        return (backgroundWidth < 0) ? background.getWidth() : backgroundWidth;
    }

    public void setBackgroundWidth(int backgroundWidth) {
        this.backgroundWidth = backgroundWidth;
    }

    public int getBackgroundHeight() {
        return (backgroundHeight < 0) ? background.getHeight() : backgroundHeight;
    }

    public void setBackgroundHeight(int backgroundHeight) {
        this.backgroundHeight = backgroundHeight;
    }

    public int getNumberOfWord() {
        return numberOfWord;
    }

    public void setNumberOfWord(int numberOfWord) {
        this.numberOfWord = Math.max(numberOfWord, 1);
    }

    public String getFontStyle() {
        return fontStyle;
    }

    public void setFontStyle(String fontStyle) {
        if(fontStyle != null && fontStyle.isEmpty()) this.fontStyle = fontStyle;
    }

    public String getImageSuffix() {
        return imageSuffix;
    }

    public void setImageSuffix(String imageSuffix) {
        if(imageSuffix == null || imageSuffix.isEmpty()) throw new NullPointerException("图片后缀名不能为空");
        this.imageSuffix = imageSuffix;;
    }

    /* ********************************************************************************************
     *
     *          链式调用
     *
     * *********************************************************************************************
     * */


    public ClickCaptchaBuilder background(BufferedImage background) {
        this.setBackground(background);
        return this;
    }

    public ClickCaptchaBuilder background(InputStream imageStream) {
        this.setBackground(imageStream);
        return this;
    }

    public ClickCaptchaBuilder background(File imageFile) {
        this.setBackground(imageFile);
        return this;
    }

    public ClickCaptchaBuilder background(String imageFilePath) {
        this.setBackground(imageFilePath);
        return this;
    }

    public ClickCaptchaBuilder numberOfWord(int numberOfWord) {
        this.setNumberOfWord(numberOfWord);
        return this;
    }

    public ClickCaptchaBuilder fontStyle(String fontStyle) {
        this.setFontStyle(fontStyle);
        return this;
    }

    public ClickCaptchaBuilder imageSuffix(String imageSuffix) {
        this.setImageSuffix(imageSuffix);
        return this;
    }
}
